﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Template;
using PathMaker;
using pvdcd;
 

namespace DynaCAD
{
    /// <summary>
    /// Interaction logic for ProjectWindow.xaml
    /// </summary>
    public partial class ProjectWindow : Window
    {
        public List<ReferanceTemplate> Templates;
        public List<pvdcd.PartWithProperty> PartWithPropertyList;
        public pvdcd.Project Project;

        AdornerLayer ADL;
        public Point? MidPointPosition;
        public Point? FirstPoint;
        public UIElement SelectedUIElement;
        public LineWithArrows.LineArrow RemoveLineArrow;
        public Pats.Pat RemovePat;
        public Path RemoveSeam;
        public List<PathMaker.Node> SeamNodes;
        public string Aim;

        public ProjectWindow()
        {
            this.MidPointPosition = null;
            this.Project = new pvdcd.Project();
            this.PartWithPropertyList = new List<pvdcd.PartWithProperty>();
            this.Templates = new List<ReferanceTemplate>();
            this.RemoveLineArrow = new LineWithArrows.LineArrow();
            this.RemovePat = new Pats.Pat();
            this.SeamNodes = new List<PathMaker.Node>();
            this.RemoveSeam = new Path();
            InitializeComponent();
                LoadTemplates();
                LoadParts();
                FillTemplateComboBox();
                FillPartTabs();
                FillEveryThing();
                IronFirstOpen();
                GeneralViewFirstOpen();
        }
        private void FillPartTabs()
        {
            TABS.Items.Clear();
            if (TemplateComboBox.Items.Count > 0)
            {
                foreach (Template.Type T in ((ReferanceTemplate)((TextBlock)(TemplateComboBox.SelectedItem)).Tag).PartTypeList)
                {
                    TabItem TI = new TabItem();
                    TI.Header = T.PartType;
                    WrapPanel WP = new WrapPanel();
                    foreach (pvdcd.PartWithProperty PWP in PartWithPropertyList)
                    {
                        if (PWP.Part.PartType.PartType == (string)(TI.Header) && PWP.Part.TemplateName == ((ReferanceTemplate)((TextBlock)(TemplateComboBox.SelectedItem)).Tag).Name)
                        {
                            Button B = new Button();
                            B.Height = 120;
                            B.Width = 72;
                            WP.Children.Add(B);
                            Viewbox VB = new Viewbox();
                            VB.Height = 120;
                            VB.Width = 72;
                            Canvas C = (Canvas)XamlEditor.CloneUIElement.cloneElement(((Canvas)PWP.Part.FrontLeftView));
                            C.Width = 1000;
                            C.Height = 1000;

                            double boundWidth = 3000;
                            double boundHeight = 3000;
                            double boundx = 0;
                            double boundy = 0;
                            C.Children.OfType<Path>().ToList<Path>().ForEach(p =>
                            {

                                if (p.Data.Bounds.X < boundWidth)
                                    boundWidth = p.Data.Bounds.X;
                                if (p.Data.Bounds.Y < boundHeight)
                                    boundHeight = p.Data.Bounds.Y;
                                if (p.Data.Bounds.X + p.Data.Bounds.Width > boundx)
                                    boundx = p.Data.Bounds.X + p.Data.Bounds.Width;
                                if (p.Data.Bounds.Y + p.Data.Bounds.Height > boundy)
                                    boundy = p.Data.Bounds.Y + p.Data.Bounds.Height;
                            });
                            C.Children.OfType<Path>().ToList<Path>().ForEach(p =>
                            {
                                p.RenderTransform = new TranslateTransform(-(boundWidth - 15), -(boundHeight - 15));

                            });
                            try
                            {
                                C.Height = boundy - boundHeight + 30;
                                C.Width = boundx - boundWidth + 30;
                            }
                            catch { }

                            VB.Child = C;
                            B.Content = VB;
                            B.Tag = PWP;
                            ContextMenu A = new ContextMenu();
                            MenuItem RemoveSeam = new MenuItem();
                            MenuItem BringSeam = new MenuItem();
                            RemoveSeam.Header = "Dikişleri Sil";
                            BringSeam.Header = "Dikişleri Ekle";
                            RemoveSeam.Click+=new RoutedEventHandler(RemoveSeamClick);
                            BringSeam.Click+=new RoutedEventHandler(BringSeamClick);
                            A.Items.Add(RemoveSeam);
                            A.Items.Add(BringSeam);
                            B.ContextMenu = A;
                            B.Click += new RoutedEventHandler(AddItem);
                        }
                    }
                    Viewbox VBD = new Viewbox();
                    VBD.Height = 60;
                    VBD.Width = 36;
                    Image Im = new Image();
                    Im.Source = new BitmapImage(new Uri("/Images/Delete.png", UriKind.Relative));
                    VBD.Child = Im;
                    Button D = new Button();
                    D.Height = 120;
                    D.Width = 72;
                    D.Content = VBD;
                    WP.Children.Add(D);
                    D.Click += new RoutedEventHandler(RemoveItem);
                    D.Tag = T;
                    TI.Content = WP;
                    TABS.Items.Add(TI);
                }
            }
        }
        private void FillTemplateComboBox()
        {
            foreach (ReferanceTemplate RT in Templates)
            {
                TextBlock T = new TextBlock();
                T.Text = RT.Name;
                TemplateComboBox.Items.Add(T);
                T.Tag = RT;
            }
            try
            {
                TemplateComboBox.SelectedItem = TemplateComboBox.Items[0];
            }
            catch { MessageBox.Show("Kayıtlı Model Bulunamadı."); }
        }
        private void LoadTemplates()
        {
            this.Templates = Karbel.Data.GetSablonlar();                
        }
 

        private void LoadParts()
        {

            this.PartWithPropertyList.Clear();

            foreach (Part part in Karbel.Data.GetParcalar())
            {
                part.DrivePart();
                this.PartWithPropertyList.Add(new pvdcd.PartWithProperty(part));
            }


            //this.PartWithPropertyList.Clear();
            //if (!System.IO.Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\Parts"))
            //{
            //    System.IO.Directory.CreateDirectory(System.IO.Directory.GetCurrentDirectory() + "\\Parts");
            //    return;
            //}
            //string[] TemplateFiles = System.IO.Directory.GetDirectories(System.IO.Directory.GetCurrentDirectory() + "\\Parts\\");
            //foreach (string Template in TemplateFiles)
            //{
            //    string[] TypeFiles = System.IO.Directory.GetDirectories(Template);
            //    foreach (string TypeFile in TypeFiles)
            //    {
            //        string[] Files = System.IO.Directory.GetFiles(TypeFile);
            //        foreach (string File in Files)
            //        {
            //            if (System.IO.Path.GetExtension(File) == ".obj")
            //            {
            //                pvdcd.Part P = pvdcd.Part.LoadPart(File);
            //                P.DrivePart();
            //                this.PartWithPropertyList.Add(new pvdcd.PartWithProperty(P));
            //            }
            //        }
            //    }
            //}
        
        }
        private void RemoveAdorner()
        {
            try
            {
                if (SelectedUIElement != null)
                {
                    Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                    if (toRemoveArray != null)
                    {
                        for (int x = 0; x < toRemoveArray.Length; x++)
                        {
                            ADL.Remove(toRemoveArray[x]);
                        }
                    }
                }
            }
            catch { }
        }
        private void AddAdorner()
        {            
            ADL = AdornerLayer.GetAdornerLayer(SelectedUIElement);
            if (SelectedUIElement is TextBox)
                ADL.Add(new XamlEditor.TextBoxAdorner((TextBox)SelectedUIElement));
            else if (SelectedUIElement is Image)
                ADL.Add(new XamlEditor.ImageAdorner((Image)SelectedUIElement));
            else if (SelectedUIElement is Path)
                ADL.Add(new XamlEditor.ResizingAdorner(SelectedUIElement));
            else if (SelectedUIElement is LineWithArrows.LineArrow)
                ADL.Add(new XamlEditor.LineArrowAdorner((LineWithArrows.LineArrow)SelectedUIElement));
            else if (SelectedUIElement is Pats.Pat)            
                ADL.Add(new XamlEditor.PatAdorner((Pats.Pat)SelectedUIElement));
            else if (SelectedUIElement is Path)
            {
                ADL.Add(new XamlEditor.ResizingAdorner(SelectedUIElement));
            }
            else
            { return; }
        }
        private void GetWidthHeightOfFrontStackPanel()
        { 
            double Width=0;
            double Height=0;
            if (Templates.Count > 0)
            {
                Width=Templates[0].ModelArka.Width;
                Height=Templates[0].ModelOn.Height;
            }
            /*if(FrontPartCanvas.Children.Count>0)
                foreach (Canvas Part in FrontPartCanvas.Children.OfType<Canvas>().ToList<Canvas>())
                { 
                    Canvas C=Part;
                    if (Part.Children.Count > 0)
                        if (Part.Children[0] is Canvas)
                            C = (Canvas)Part.Children[0];
                    if (C.Children.Count > 0)
                        if (Part.Children[0] is Canvas)
                            C = (Canvas)Part.Children[0];
                    if (C.Children.Count > 0)
                        if (Part.Children[0] is Canvas)
                            C = (Canvas)Part.Children[0];
                    if (C.Children.Count > 0)
                        if (Part.Children[0] is Canvas)
                            C = (Canvas)Part.Children[0];
                    foreach (Path p in C.Children.OfType<Path>().ToList<Path>())
                    { 
                if(Width!>)
                    }
                }*/
        }
        private Canvas JoinPartsFront(List<pvdcd.Part> PL)
        {
            Canvas C = new Canvas();
            foreach (pvdcd.Part prt in PL)
            {
                C.Children.Add(prt.GetFrontLeftView());
                if (prt.PartType.LeftRight)
                    C.Children.Add(prt.GetFrontRightView());
            }
            return C;
        }
        private Canvas JoinPartsBack(List<pvdcd.Part> PL)
        {
            Canvas C = new Canvas();
            foreach (pvdcd.Part prt in PL)
            {
                if (prt.PartType.FrontBack)
                {
                    C.Children.Add(prt.GetBackLeftView());
                    if (prt.PartType.LeftRight)
                        C.Children.Add(prt.GetBackRightView());
                }
            }
            return C;
        }
        private void AddTextBoxEvents(Canvas C)
        {
            C.Children.OfType<TextBox>().ToList<TextBox>().ForEach(T =>
            {
                //
                MenuItem MenuItem1 = new MenuItem();
                MenuItem1.Header = "Notu Sil";
                MenuItem1.Click += new RoutedEventHandler(NoteRemove);
                MenuItem1.Icon = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("Images/Delete.png", UriKind.Relative))
                };
                MenuItem1.Height = 20;
                T.ContextMenu = new ContextMenu();
                T.ContextMenu.Items.Add(MenuItem1);
                //
                T.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(TextBoxMouseLeftButtonDown);
            });
        
        }

        #region GeneralView
        private void FillGeneralViewPartCanvas()
        {
            Canvas FrontBase = new Canvas();
            Canvas BackBase = new Canvas();
            Canvas Front = new Canvas();
            Canvas Back = new Canvas();
            Canvas GeneralView = new Canvas();
            double WidthCOn = 909;
            double HeightCOn = 1350;
            if (Templates.Count > 0)
            {
                WidthCOn = Templates[0].ModelOn.Width;
                HeightCOn = Templates[0].ModelOn.Height;
            }
            double WidthCArka = 909;
            double HeightCArka = 1350;
            if (Templates.Count > 0)
            {
                WidthCArka = Templates[0].ModelArka.Width;
                HeightCArka = Templates[0].ModelArka.Height;
            }

            GeneralView.Width = 793.2774;
            GeneralView.Height = 1122.72;
            foreach (pvdcd.PartWithProperty PW in this.Project.PartList)
            {
                PW.Seam = !PW.Seam;
                PW.Seam = !PW.Seam;
                FrontBase.Children.Add(PW.Part.GetFrontLeftView());
                if (PW.Part.PartType.LeftRight)
                    FrontBase.Children.Add(PW.Part.GetFrontRightView());
                if (PW.Part.PartType.FrontBack)
                    BackBase.Children.Add(PW.Part.GetBackLeftView());
                if (PW.Part.PartType.FrontBack && PW.Part.PartType.LeftRight)
                    BackBase.Children.Add(PW.Part.GetBackRightView());
            }
            Front.Children.Add(FrontBase);
            Back.Children.Add(BackBase);
            FrontBase.RenderTransform = new ScaleTransform(Math.Min(GeneralView.Width / WidthCOn, GeneralView.Height / HeightCOn), Math.Min(GeneralView.Width / WidthCOn, GeneralView.Height / HeightCOn));
            BackBase.RenderTransform = new ScaleTransform(Math.Min(GeneralView.Width / WidthCArka, GeneralView.Height / HeightCArka), Math.Min(GeneralView.Width / WidthCArka, GeneralView.Height / HeightCArka));
            GeneralView.Children.Add(Front);
            GeneralView.Children.Add(Back);
            Front.RenderTransform = new ScaleTransform(0.5,0.5);
            Back.RenderTransform = new ScaleTransform(0.5, 0.5);
            Canvas.SetLeft(Front, GeneralView.Width / 20);
            Canvas.SetLeft(Back, GeneralView.Width / 20 * 9);
            Canvas.SetTop(Front, GeneralView.Height / 6);
            Canvas.SetTop(Back, GeneralView.Height / 3);
            GeneralViewParts.Children.Clear();
            GeneralViewParts.Children.Add(GeneralView);
            if(this.Project.PartList.Exists(p=> p.Part.PartType.PartType=="Sev Böcek"))
            {
                pvdcd.Part P=this.Project.PartList.Find(p => p.Part.PartType.PartType == "Sev Böcek").Part;
                if (P.DetailViews.Count > 0)
                {
                    double Scale=1;
                    GeneralViewParts.Children.Add(P.DetailViews[0].Detail);
                    Canvas.SetTop(P.DetailViews[0].Detail, 730);
                    Canvas.SetLeft(P.DetailViews[0].Detail, 56);
                    if (P.DetailViews[0].Detail.Width > 170)
                        Scale = 170 / P.DetailViews[0].Detail.Width;
                    Scale = Math.Min(Scale, 170 / P.DetailViews[0].Detail.Height);
                    P.DetailViews[0].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                    Path Sev = new Path();
                    TextBlock T = new TextBlock();
                    T.Text = "Sev Böcek İçten ve Dıştan Görünüm";
                    T.Foreground = Brushes.Red;
                    T.FontSize = 13;
                    Canvas.SetTop(T, 685);
                    Canvas.SetLeft(T, 30);
                    GeometryGroup GG = new GeometryGroup();
                    RectangleGeometry RG = new RectangleGeometry();
                    LineGeometry Line = new LineGeometry(new Point(26,710),new Point(256,710));
                    GG.Children.Add(Line);
                    GG.Children.Add(RG);
                    RG.Rect = new Rect(new Point(26,680),new Point(256,920));
                    Sev.Data = GG;
                    Sev.Stroke = Brushes.Black;
                    GeneralViewParts.Children.Add(Sev);
                    GeneralViewParts.Children.Add(T);

                }
            }
        }
        private void FillGeneralViewTemplate()
        {
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GeneralViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p =>
            {
                if (p.Name == "Date")
                {
                    DateTime T = DateTime.Now;
                    p.Text = "Tarih: " + String.Format("{0:dd/MM/yyyy}", T);
                }
                else if (p.Name == "Customer_Representative")
                {
                    p.Text = Customer_Representative.Text;
                }
                else if (p.Name == "Plan")
                {
                    p.Text = Plan.Text;
                }
                else if (p.Name == "Technical_Drawing")
                {
                    p.Text = Technical_Drawing.Text;
                }
            });
            Template.RenderTransform = new ScaleTransform(793.2774 / Template.Width, 1122.72 / Template.Height);
            GeneralViewTemplateCanvas.Children.Clear();
            GeneralViewTemplateCanvas.Children.Add(Template);
        }
        private void FillGeneralView()
        {
            FillGeneralViewPartCanvas();
            FillGeneralViewTemplate();
        }
        private void GeneralViewFirstOpen()
        {
            RichTextBox T = new RichTextBox();
            T.AppendText("......");
            T.FontSize = 11;
            Canvas.SetTop(T, 115);
            Canvas.SetLeft(T, 175);
            T.Width = 600;
            ((Canvas)(GeneralViewShapes.Children[0])).Children.Add(T);
            T.BorderBrush = Brushes.White;

            RichTextBox T1 = new RichTextBox();
            T1.AppendText("......");
            T1.FontSize = 11;
            Canvas.SetTop(T1, 134);
            Canvas.SetLeft(T1, 175);
            T1.Width = 600;
            ((Canvas)(GeneralViewShapes.Children[0])).Children.Add(T1);
            T1.BorderBrush = Brushes.White;

            RichTextBox T2 = new RichTextBox();
            T2.AppendText("......");
            T2.FontSize = 11;
            Canvas.SetTop(T2, 153);
            Canvas.SetLeft(T2, 175);
            T2.Width = 600;
            ((Canvas)(GeneralViewShapes.Children[0])).Children.Add(T2);
            T2.BorderBrush = Brushes.White;

            RichTextBox T3 = new RichTextBox();
            T3.AppendText("......");
            T3.FontSize = 11;
            Canvas.SetTop(T3, 172);
            Canvas.SetLeft(T3, 110);
            T3.Width = 150;
            ((Canvas)(GeneralViewShapes.Children[0])).Children.Add(T3);
            T3.BorderBrush = Brushes.White;

            RichTextBox T4 = new RichTextBox(); 
            T4.AppendText("......");
            T4.FontSize = 11;
            Canvas.SetTop(T4, 172);
            Canvas.SetLeft(T4, 363);
            T4.Width = 150;
            ((Canvas)(GeneralViewShapes.Children[0])).Children.Add(T4);
            T4.BorderBrush = Brushes.White;

            RichTextBox T5 = new RichTextBox();
            T5.AppendText("......");
            T5.FontSize = 11;
            Canvas.SetTop(T5, 172);
            Canvas.SetLeft(T5, 615);
            T5.Width = 150;
            ((Canvas)(GeneralViewShapes.Children[0])).Children.Add(T5);
            T5.BorderBrush = Brushes.White;
        
        }
        private void GeneralViewGridMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(GeneralViewShapes, GeneralViewStackPanel, GeneralViewGrid);
        }
        private void GeneralViewGridMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(GeneralViewStackPanel, e.Delta > 0, e.GetPosition(GeneralViewStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void GeneralViewGridMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(GeneralViewShapes, GeneralViewStackPanel);
        }
        private void GeneralViewGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(GeneralViewShapes);
        }
        #endregion GeneralView

        #region Collar
        private void FillCollarPartCanvas()
        {
            CollarParts.Children.Clear();
            foreach (pvdcd.PartWithProperty PW in this.Project.PartList)
            {
                if (PW.Part.PartType.PartType == "Yaka")
                {
                    if (PW.Part.DetailViews.Count > 0)
                    {
                        double Scale = 1;
                        CollarParts.Children.Add(PW.Part.DetailViews[0].Detail);
                        Canvas.SetTop(PW.Part.DetailViews[0].Detail, 100);
                        if (PW.Part.DetailViews[0].Detail.Width > 740)
                            Scale = 740/PW.Part.DetailViews[0].Detail.Width;
                        if (PW.Part.DetailViews[0].Detail.Height > 190)
                        { 
                            Scale=Math.Min(Scale,190/PW.Part.DetailViews[0].Detail.Height);
                        }
                        PW.Part.DetailViews[0].Detail.RenderTransform=new ScaleTransform(Scale,Scale);
                        Canvas.SetLeft(PW.Part.DetailViews[0].Detail, (794 - PW.Part.DetailViews[0].Detail.Width*Scale) / 2);
                        if (PW.Part.DetailViews[0].IsTable)
                        {
                            FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                            FDSV.IsToolBarVisible = false;
                            FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                            FDSV.PreviewMouseWheel += new MouseWheelEventHandler(CollarGridMouseWheel);
                            FDSV.Zoom = 1;
                            FDSV.ZoomIncrement = 0.0000001;
                            Table T = PW.Part.DetailViews[0].DetailTable;
                            FlowDocument FD=new FlowDocument();
                            FD.Blocks.Add(T);
                            FDSV.Document = FD;
                            FDSV.RenderTransform = new ScaleTransform(1.3, 1);
                            Canvas.SetTop(FDSV, 280);
                            Canvas.SetLeft(FDSV, 5);
                            CollarParts.Children.Add(FDSV);
                        }
                    }
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if(this.Project.PartList.Exists(p=>p.Part.PartType.PartType=="Yaka Kuşak Ucu"))
                    {
                    PL.Add(this.Project.PartList.Find(p=>p.Part.PartType.PartType=="Yaka Kuşak Ucu").Part);
                    }
                    Canvas C=JoinPartsFront(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                   
                    Canvas.SetTop(C,320);
                    Canvas.SetLeft(C,600);
                    C.RenderTransform = new ScaleTransform(Math.Min(160/C.Width,160/C.Height),Math.Min(160/C.Width,160/C.Height));
                    CollarParts.Children.Add(C);
                    FlowDocumentScrollViewer FDSVC = new FlowDocumentScrollViewer();
                    FDSVC.IsToolBarVisible = false;
                    FDSVC.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                    FlowDocument FDC = new FlowDocument();
                    FDC.Blocks.Add(this.Project.CollarTable);
                    FDSVC.Document = FDC;
                    CollarParts.Children.Add(FDSVC);
                    FDSVC.PreviewMouseWheel += new MouseWheelEventHandler(CollarGridMouseWheel);
                    FDSVC.Zoom = 1;
                    FDSVC.ZoomIncrement = 0.0000001;
                    if (PW.Part.DetailViews.Count > 1)
                    {
                        Canvas.SetTop(FDSVC, 370 + PW.Part.DetailViews[0].TableVeriables.Count * 15);
                    }
                    else
                    {
                        Canvas.SetTop(FDSVC, 370);
                    }
                    Canvas.SetLeft(FDSVC, 5);
                    FDSVC.RenderTransform = new ScaleTransform(1.3, 1);
                    Path R = new Path();
                    RectangleGeometry RG = new RectangleGeometry(new Rect(new Point(200,500),new Point(600,630)));
                    R.Stroke = Brushes.Black;
                    R.Data = RG;
                    TextBlock Yaka = new TextBlock();
                    Yaka.Text = "Yaka Telası       :";
                    Yaka.FontSize = 25;
                    Yaka.Foreground = Brushes.Red;
                    Canvas.SetLeft(Yaka, 205);
                    Canvas.SetTop(Yaka, 500);
                    TextBlock Poli = new TextBlock();
                    Poli.Text = "Poligot Telası   :";
                    Poli.FontSize = 25;
                    Poli.Foreground = Brushes.Red;
                    Canvas.SetLeft(Poli, 205);
                    Canvas.SetTop(Poli, 525);
                    TextBlock Kusak = new TextBlock();
                    Kusak.Text = "Kuşak Telası     :";
                    Kusak.FontSize = 25;
                    Kusak.Foreground = Brushes.Red;
                    Canvas.SetLeft(Kusak, 205);
                    Canvas.SetTop(Kusak, 550);
                    TextBlock Man = new TextBlock();
                    Man.Text = "Manşet Telası   :";
                    Man.FontSize = 25;
                    Man.Foreground = Brushes.Red;
                    Canvas.SetLeft(Man, 205);
                    Canvas.SetTop(Man, 575);

                    CollarParts.Children.Add(R);
                    CollarParts.Children.Add(Poli);
                    CollarParts.Children.Add(Kusak);
                    CollarParts.Children.Add(Man);
                    CollarParts.Children.Add(Yaka);
                }
                else if (PW.Part.PartType.PartType == "Manşet")
                {
                    if (PW.Part.DetailViews.Count > 0)
                    {
                        double Scale = 1;
                        CollarParts.Children.Add(PW.Part.DetailViews[0].Detail);
                        Canvas.SetTop(PW.Part.DetailViews[0].Detail, 650);
                        if (PW.Part.DetailViews[0].Detail.Width > 740)
                            Scale = 740 / PW.Part.DetailViews[0].Detail.Width;
                        if (PW.Part.DetailViews[0].IsTable)
                        {
                            if (PW.Part.DetailViews[0].Detail.Height + PW.Part.DetailViews[0].TableVeriables.Count * 15 + 45 > 420)
                            {
                                Scale = Math.Min((420 - (PW.Part.DetailViews[0].TableVeriables.Count * 15 + 45)) / PW.Part.DetailViews[0].Detail.Height, Scale);
                                PW.Part.DetailViews[0].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                                Canvas.SetLeft(PW.Part.DetailViews[0].Detail, (794 - (PW.Part.DetailViews[0].Detail.Height * Scale)) / 2);
                                FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                                FDSV.IsToolBarVisible = false;
                                FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                                FDSV.PreviewMouseWheel += new MouseWheelEventHandler(CollarGridMouseWheel);
                                FDSV.Zoom = 1;
                                FDSV.ZoomIncrement = 0.0000001;
                                Table T = PW.Part.DetailViews[0].DetailTable;
                                FlowDocument FD = new FlowDocument();
                                FD.Blocks.Add(T);
                                FDSV.Document = FD;
                                Canvas.SetTop(FDSV, 650+PW.Part.DetailViews[0].Detail.Height*Scale);
                                Canvas.SetLeft(FDSV, 5);
                                CollarParts.Children.Add(FDSV);
                            }
                            else
                            {
                                PW.Part.DetailViews[0].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                                Canvas.SetLeft(PW.Part.DetailViews[0].Detail, (794 - (PW.Part.DetailViews[0].Detail.Height * Scale)) / 2);
                                FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                                FDSV.IsToolBarVisible = false;
                                FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                                FDSV.PreviewMouseWheel += new MouseWheelEventHandler(CollarGridMouseWheel);
                                FDSV.Zoom = 1;
                                FDSV.ZoomIncrement = 0.0000001;
                                Table T = PW.Part.DetailViews[0].DetailTable;
                                FlowDocument FD = new FlowDocument();
                                FD.Blocks.Add(T);
                                FDSV.Document = FD;
                                Canvas.SetTop(FDSV, 650 + PW.Part.DetailViews[0].Detail.Height * Scale);
                                Canvas.SetLeft(FDSV, 5);
                                CollarParts.Children.Add(FDSV);
                            }
                        }
                        else
                        {
                            if (PW.Part.DetailViews[0].Detail.Height>420)
                            {
                                Scale = Math.Min(420  / PW.Part.DetailViews[0].Detail.Height, Scale);
                                PW.Part.DetailViews[0].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                                Canvas.SetLeft(PW.Part.DetailViews[0].Detail, (794 - (PW.Part.DetailViews[0].Detail.Height * Scale)) / 2);
                            }
                            else
                            {
                                PW.Part.DetailViews[0].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                                Canvas.SetLeft(PW.Part.DetailViews[0].Detail, (794 - (PW.Part.DetailViews[0].Detail.Height * Scale)) / 2);
                            }
                        }
                    }
                }
            }
        
        }
        private void FillCollarTemplate()
        {
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("DetailViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p =>
            {
                if (p.Name == "DetailName")
                {
                    p.Text = "Gömlek Yaka+Manşet";
                }
                else if (p.Name == "Customer_RepresentativeDetail")
                {
                    p.Text = Customer_Representative.Text;
                }
                else if (p.Name == "PlanDetail")
                {
                    p.Text = Plan.Text;
                }
                else if (p.Name == "Technical_DrawingDetail")
                {
                    p.Text = Technical_Drawing.Text;
                }
            });
            CollarTemplateCanvas.Children.Clear();
            CollarTemplateCanvas.Children.Add(Template);
        }
        private void FillCollar()
        {
            FillCollarPartCanvas();
            FillCollarTemplate();
        }
        private void CollarGridMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(CollarShapes, CollarStackPanel, CollarGrid);
        }
        private void CollarGridMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(CollarStackPanel, e.Delta > 0, e.GetPosition(CollarStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void CollarGridMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(CollarShapes, CollarStackPanel);
        }
        private void CollarGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(CollarShapes);
        }
        #endregion Collar

        #region FrontSize
        private void FillFrontSizePartCanvas()
        {
            FrontSizeParts.Children.Clear();
            foreach (pvdcd.PartWithProperty PW in this.Project.PartList)
            {
                if (PW.Part.PartType.PartType == "Cep")
                {
                    double Scale = 1;
                    Canvas C=XO1.CanvasOperations.PreparedCanvas(PW.Part.FrontLeftView);
                    FrontSizeParts.Children.Add(C);
                    Canvas.SetTop(C, 150);
                    Canvas.SetLeft(C, 80);
                    if (C.Height > 210)
                        Scale = 210 / C.Height;
                    Scale = Math.Min(175 / C.Width, Scale);
                    C.RenderTransform = new ScaleTransform(Scale, Scale);
                    FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                    FDSV.IsToolBarVisible = false;
                    FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                    FDSV.PreviewMouseWheel += new MouseWheelEventHandler(FrontSizeGridMouseWheel);
                    FDSV.Zoom = 1;
                    FDSV.ZoomIncrement = 0.0000001;
                    Table T = this.Project.PocketTable;
                    FlowDocument FD = new FlowDocument();
                    FD.Blocks.Add(T);
                    FDSV.Document = FD;
                    FDSV.RenderTransform = new ScaleTransform(1.3, 1);
                    Canvas.SetTop(FDSV, 130);
                    Canvas.SetLeft(FDSV, 250);
                    FrontSizeParts.Children.Add(FDSV);
                    Path PCep = new Path();
                    GeometryGroup GCep = new GeometryGroup();
                    RectangleGeometry RCep = new RectangleGeometry();
                    RCep.Rect = new Rect(new Point(26,100),new Point(768,310));
                    LineGeometry LCep = new LineGeometry(new Point(26,120), new Point(768,120));
                    LineGeometry LCep1 = new LineGeometry(new Point(230,120), new Point(230,310));
                    TextBlock Cep = new TextBlock();
                    Cep.Text = "Cep Detayı";
                    Cep.FontSize = 15;
                    Cep.Foreground = Brushes.Red;
                    Canvas.SetLeft(Cep, 360);
                    Canvas.SetTop(Cep, 100);
                    GCep.Children.Add(RCep);
                    GCep.Children.Add(LCep);
                    GCep.Children.Add(LCep1);
                    PCep.Data = GCep;
                    PCep.Stroke = Brushes.Black;
                    FrontSizeParts.Children.Add(PCep);
                    FrontSizeParts.Children.Add(Cep);
                }
                else if (PW.Part.PartType.PartType == "Pat")
                {
                    if (PW.Part.DetailViews.Count > 0)
                    {
                        double Scale = 1;
                        FrontSizeParts.Children.Add(PW.Part.DetailViews[0].Detail);
                        Canvas.SetTop(PW.Part.DetailViews[0].Detail, 360);

                        if (PW.Part.DetailViews[0].Detail.Height > 520)
                            Scale = (520) / PW.Part.DetailViews[0].Detail.Height;
                        Scale = Math.Min(270 / PW.Part.DetailViews[0].Detail.Width, Scale);
                        PW.Part.DetailViews[0].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                        Canvas.SetLeft(PW.Part.DetailViews[0].Detail, 13+(300-(PW.Part.DetailViews[0].Detail.Width*Scale))/2);
                        if (PW.Part.DetailViews[0].IsTable)
                        {
                            FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                            FDSV.IsToolBarVisible = false;
                            FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                            FDSV.PreviewMouseWheel += new MouseWheelEventHandler(FrontSizeGridMouseWheel);
                            FDSV.Zoom = 1;
                            FDSV.ZoomIncrement = 0.0000001;
                            FDSV.RenderTransform = new ScaleTransform(1.3, 1);
                            Table T = PW.Part.DetailViews[0].DetailTable;
                            FlowDocument FD = new FlowDocument();
                            FD.Blocks.Add(T);
                            FDSV.Document = FD;
                            Canvas.SetTop(FDSV, 880);
                            Canvas.SetLeft(FDSV, 50);
                            FrontSizeParts.Children.Add(FDSV);
                        }
                        Path P = new Path();
                        GeometryGroup G = new GeometryGroup();
                        RectangleGeometry R= new RectangleGeometry();
                        LineGeometry L = new LineGeometry(new Point(18,340),new Point(313,340));
                        TextBlock TB = new TextBlock();
                        TB.Text = "Pat İlik ve Cep Yeri Ölçüleri";
                        Canvas.SetTop(TB, 320);
                        Canvas.SetLeft(TB, 25);
                        TB.FontSize = 15;
                        TB.Foreground = Brushes.Red;
                        G.Children.Add(R);
                        G.Children.Add(L);
                        R.Rect = new Rect(new Point(18,320), new Point(313,885));
                        P.Data = G;
                        P.Stroke = Brushes.Black;
                        FrontSizeParts.Children.Add(P);
                        FrontSizeParts.Children.Add(TB);

                    }
                    if (PW.Part.DetailViews.Count > 1)
                    {
                        double Scale = 1;
                        FrontSizeParts.Children.Add(PW.Part.DetailViews[1].Detail);
                        Canvas.SetTop(PW.Part.DetailViews[1].Detail, 360);
                        if (PW.Part.DetailViews[1].Detail.Height > 520)
                            Scale = 520 / PW.Part.DetailViews[1].Detail.Height;
                        Scale = Math.Min(200 / PW.Part.DetailViews[1].Detail.Width, Scale);
                        PW.Part.DetailViews[1].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                        Canvas.SetLeft(PW.Part.DetailViews[1].Detail, 313 + (230 - (PW.Part.DetailViews[1].Detail.Width * Scale)) / 2);
                        Path P = new Path();
                        GeometryGroup G = new GeometryGroup();
                        RectangleGeometry R = new RectangleGeometry();
                        LineGeometry L = new LineGeometry(new Point(313, 340), new Point(533, 340));
                        TextBlock TB = new TextBlock();
                        TB.Text = "Pat İçten Görünüm";
                        Canvas.SetTop(TB, 320);
                        Canvas.SetLeft(TB, 323);
                        TB.FontSize = 15;
                        TB.Foreground = Brushes.Red;
                        G.Children.Add(R);
                        G.Children.Add(L);
                        R.Rect = new Rect(new Point(313, 320), new Point(533, 885));
                        P.Data = G;
                        P.Stroke = Brushes.Black;
                        FrontSizeParts.Children.Add(P);
                        FrontSizeParts.Children.Add(TB);
                    }
                    if (PW.Part.DetailViews.Count > 2)
                    {
                        double Scale = 1;
                        FrontSizeParts.Children.Add(PW.Part.DetailViews[2].Detail);
                        Canvas.SetTop(PW.Part.DetailViews[2].Detail, 360);
                        if (PW.Part.DetailViews[2].Detail.Height > 520)
                            Scale = 520 / PW.Part.DetailViews[2].Detail.Height;
                        Scale = Math.Min(200 / PW.Part.DetailViews[2].Detail.Width, Scale);
                        PW.Part.DetailViews[2].Detail.RenderTransform = new ScaleTransform(Scale, Scale);
                        Canvas.SetLeft(PW.Part.DetailViews[2].Detail, 543 + (230 - (PW.Part.DetailViews[2].Detail.Width * Scale)) / 2);
                        Path P = new Path();
                        GeometryGroup G = new GeometryGroup();
                        RectangleGeometry R = new RectangleGeometry();
                        LineGeometry L = new LineGeometry(new Point(533, 340), new Point(776, 340));
                        TextBlock TB = new TextBlock();
                        TB.Text = "Pat Katlaması";
                        Canvas.SetTop(TB, 320);
                        Canvas.SetLeft(TB, 543);
                        TB.FontSize = 15;
                        TB.Foreground = Brushes.Red;
                        G.Children.Add(R);
                        G.Children.Add(L);
                        R.Rect = new Rect(new Point(533, 320), new Point(776, 885));
                        P.Data = G;
                        P.Stroke = Brushes.Black;
                        FrontSizeParts.Children.Add(P);
                        FrontSizeParts.Children.Add(TB);
                    }
                }
            }
        
        }
        private void FillFrontSizeTemplate()
        {
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("DetailViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p =>
            {
                if (p.Name == "DetailName")
                {
                    p.Text = "Gömlek Ön Beden";
                }
                else if (p.Name == "Customer_RepresentativeDetail")
                {
                    p.Text = Customer_Representative.Text;
                }
                else if (p.Name == "PlanDetail")
                {
                    p.Text = Plan.Text;
                }
                else if (p.Name == "Technical_DrawingDetail")
                {
                    p.Text = Technical_Drawing.Text;
                }
            });
            FrontSizeTemplateCanvas.Children.Clear();
            FrontSizeTemplateCanvas.Children.Add(Template);
        
        }
        private void FillFrontSize()
        {
            FillFrontSizePartCanvas();
            FillFrontSizeTemplate();
        }
        private void FrontSizeGridMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(FrontSizeShapes, FrontSizeStackPanel, FrontSizeGrid);
        }
        private void FrontSizeGridMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(FrontSizeStackPanel, e.Delta > 0, e.GetPosition(FrontSizeStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void FrontSizeGridMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(FrontSizeShapes, FrontSizeStackPanel);
        }
        private void FrontSizeGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(FrontSizeShapes);
        }
        #endregion FrontSize

        #region BackSize1
        private void FillBackSize1PartCanvas()
        {
            BackSizeParts1.Children.Clear();
            foreach (pvdcd.PartWithProperty PW in this.Project.PartList)
            {
                if (PW.Part.PartType.PartType == "Beden")
                {
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Etek"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Etek").Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Roba"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Roba").Part);
                    Canvas C = JoinPartsBack(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                    double Scale = 1;
                    BackSizeParts1.Children.Add(C);
                    Canvas.SetTop(C, 140);
                    if (C.Height > 360)
                        Scale = 360 / C.Height;
                    Scale = Math.Min(350 / C.Width, Scale);
                    C.RenderTransform = new ScaleTransform(Scale, Scale);
                    Canvas.SetLeft(C, (400 - (C.Width * Scale)) / 2);
                    Canvas C2 = (Canvas)XamlEditor.CloneUIElement.cloneElement(C);
                    Canvas.SetLeft(C2, 400+(400 - (C.Width * Scale)) / 2);
                    BackSizeParts1.Children.Add(C2);
                    Path P = new Path();
                    LineGeometry L1 = new LineGeometry(new Point(13,120), new Point(781,120));
                    LineGeometry L2 = new LineGeometry(new Point(13,530), new Point(781,530));
                    LineGeometry L3 = new LineGeometry(new Point(400,96), new Point(400,530));
                    GeometryGroup G=new GeometryGroup();
                    G.Children.Add(L1);
                    G.Children.Add(L2);
                    G.Children.Add(L3);
                    P.Data=G;
                    P.Stroke=Brushes.Black;
                    TextBlock T1 = new TextBlock();
                    T1.Text = "Arka Dıştan Görünüm";
                    T1.FontSize = 15;
                    T1.Foreground = Brushes.Red;
                    Canvas.SetLeft(T1,140);
                    Canvas.SetTop(T1,96);
                    TextBlock T2 = new TextBlock();
                    T2.Text = "Arka İçten Görünüm";
                    T2.FontSize = 15;
                    T2.Foreground = Brushes.Red;
                    Canvas.SetLeft(T2, 540);
                    Canvas.SetTop(T2, 96);
                    BackSizeParts1.Children.Add(P);
                    BackSizeParts1.Children.Add(T1);
                    BackSizeParts1.Children.Add(T2);
                    
                }
                else if (PW.Part.PartType.PartType == "Yaka")
                {
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Yaka Kuşak Ucu"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Yaka Kuşak Ucu").Part);
                    Canvas C = JoinPartsFront(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                    double Scale = 1;
                    BackSizeParts1.Children.Add(C);
                    Canvas.SetTop(C, 550);
                    if (C.Height > 180)
                        Scale = 180 / C.Height;
                    Scale = Math.Min(760 / C.Width, Scale);
                    C.RenderTransform = new ScaleTransform(Scale, Scale);
                    Canvas.SetLeft(C, (794 - (C.Width * Scale)) / 2);
                    Path P = new Path();
                    LineGeometry L1 = new LineGeometry(new Point(13, 750), new Point(781, 750));
                    P.Data = L1;
                    P.Stroke = Brushes.Black;
                    BackSizeParts1.Children.Add(P);

                }
                else if (PW.Part.PartType.PartType == "Apatura")
                {
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Kol Pili"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Kol Pili").Part);
                    Canvas C = JoinPartsFront(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                    double Scale = 3;
                    BackSizeParts1.Children.Add(C);
                    Canvas.SetTop(C, 900);
                    if (C.Height * Scale > 250)
                    {
                        Scale = Scale * (250 / (C.Height * Scale));
                    }
                    Scale = Math.Min(760 / C.Width, Scale);
                    ScaleTransform ST = new ScaleTransform(Scale / 2, Scale / 2);
                    RotateTransform RT = new RotateTransform();
                    Vector V = new Vector(1,0); 
                    Point P1 = new Point(PW.Part.PartType.FrontLeftPoints[0].X, PW.Part.PartType.FrontLeftPoints[0].Y);
                    Point P2 = new Point(PW.Part.PartType.FrontLeftPoints[1].X, PW.Part.PartType.FrontLeftPoints[1].Y);
                    double Angle = Vector.AngleBetween(new Vector(P1.X-P2.X,P1.Y-P2.Y),V);
                    if (Angle > 90)
                        Angle = 180 - Angle;
                    RT.Angle = -30;
                    TransformGroup TG = new TransformGroup();
                    TG.Children.Add(RT);
                    TG.Children.Add(ST);
                    C.RenderTransform = TG;
                    Canvas.SetLeft(C, (794 - (C.Width * Scale/2)) / 2);
                }
            }
        }
        private void FillBackSize1Template()
        {
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("DetailViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p =>
            {
                if (p.Name == "DetailName")
                {
                    p.Text = "Gömlek Arka Beden";
                }
                else if (p.Name == "Customer_RepresentativeDetail")
                {
                    p.Text = Customer_Representative.Text;
                }
                else if (p.Name == "PlanDetail")
                {
                    p.Text = Plan.Text;
                }
                else if (p.Name == "Technical_DrawingDetail")
                {
                    p.Text = Technical_Drawing.Text;
                }
            });
            BackSizeTemplateCanvas.Children.Clear();
            BackSizeTemplateCanvas.Children.Add(Template);
        
        }
        private void FillBackSize1()
        { 
            FillBackSize1PartCanvas();
            FillBackSize1Template();
        }
        private void BackSizeGrid1MouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(BackSizeShapes1, BackSizeStackPanel1, BackSizeGrid1);
        }
        private void BackSizeGrid1MouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(BackSizeStackPanel1, e.Delta > 0, e.GetPosition(BackSizeStackPanel1));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void BackSizeGrid1MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(BackSizeShapes1, BackSizeStackPanel1);
        }
        private void BackSizeGrid1MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(BackSizeShapes1);
        }
        #endregion BackSize1

        #region BackSize2
        private void FillBackSize2PartCanvas()
        {
            BackSizeParts2.Children.Clear();
            foreach (pvdcd.PartWithProperty PW in this.Project.PartList)
            {
                if (PW.Part.PartType.PartType == "Beden")
                {
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Etek"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Etek").Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Roba"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Roba").Part);
                    Canvas C = JoinPartsBack(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                    double Scale = 1;
                    BackSizeParts2.Children.Add(C);
                    Canvas.SetTop(C, 140);
                    if (C.Height > 360)
                        Scale = 360 / C.Height;
                    Scale = Math.Min(350 / C.Width, Scale);
                    C.RenderTransform = new ScaleTransform(Scale, Scale);
                    Canvas.SetLeft(C, (400 - (C.Width * Scale)) / 2);
                    Canvas C2 = (Canvas)XamlEditor.CloneUIElement.cloneElement(C);
                    Canvas.SetLeft(C2, 400 + (400 - (C.Width * Scale)) / 2);
                    BackSizeParts2.Children.Add(C2);
                    Path P = new Path();
                    LineGeometry L1 = new LineGeometry(new Point(13, 120), new Point(781, 120));
                    LineGeometry L2 = new LineGeometry(new Point(13, 530), new Point(781, 530));
                    LineGeometry L3 = new LineGeometry(new Point(400, 96), new Point(400, 530));
                    GeometryGroup G = new GeometryGroup();
                    G.Children.Add(L1);
                    G.Children.Add(L2);
                    G.Children.Add(L3);
                    P.Data = G;
                    P.Stroke = Brushes.Black;
                    TextBlock T1 = new TextBlock();
                    T1.Text = "Arka Dıştan Görünüm";
                    T1.FontSize = 15;
                    T1.Foreground = Brushes.Red;
                    Canvas.SetLeft(T1, 140);
                    Canvas.SetTop(T1, 96);
                    TextBlock T2 = new TextBlock();
                    T2.Text = "Arka İçten Görünüm";
                    T2.FontSize = 15;
                    T2.Foreground = Brushes.Red;
                    Canvas.SetLeft(T2, 540);
                    Canvas.SetTop(T2, 96);
                    BackSizeParts2.Children.Add(P);
                    BackSizeParts2.Children.Add(T1);
                    BackSizeParts2.Children.Add(T2);

                }
                else if (PW.Part.PartType.PartType == "Yaka")
                {
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Yaka Kuşak Ucu"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Yaka Kuşak Ucu").Part);
                    Canvas C = JoinPartsFront(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                    double Scale = 1;
                    BackSizeParts2.Children.Add(C);
                    Canvas.SetTop(C, 550);
                    if (C.Height > 180)
                        Scale = 180 / C.Height;
                    Scale = Math.Min(760 / C.Width, Scale);
                    C.RenderTransform = new ScaleTransform(Scale, Scale);
                    Canvas.SetLeft(C, 26);
                    Path P = new Path();
                    LineGeometry L1 = new LineGeometry(new Point(13, 750), new Point(781, 750));
                    P.Data = L1;
                    P.Stroke = Brushes.Black;
                    BackSizeParts2.Children.Add(P);

                    FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                    FDSV.IsToolBarVisible = false;
                    FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                    FDSV.PreviewMouseWheel += new MouseWheelEventHandler(BackSizeGrid2MouseWheel);
                    FDSV.Zoom = 1;
                    FDSV.ZoomIncrement = 0.0000001;
                    Table T = this.Project.CollarLableTable;
                    FlowDocument FD = new FlowDocument();
                    FD.Blocks.Add(T);
                    FDSV.Document = FD;
                    FDSV.RenderTransform = new ScaleTransform(1.2, 1);
                    Canvas.SetTop(FDSV, 550);
                    Canvas.SetLeft(FDSV, 300);
                    BackSizeParts2.Children.Add(FDSV);
                }
                else if (PW.Part.PartType.PartType == "Apatura")
                {
                    List<pvdcd.Part> PL = new List<pvdcd.Part>();
                    PL.Add(PW.Part);
                    if (this.Project.PartList.Exists(p => p.Part.PartType.PartType == "Kol Pili"))
                        PL.Add(this.Project.PartList.Find(p => p.Part.PartType.PartType == "Kol Pili").Part);
                    Canvas C = JoinPartsFront(PL);
                    C = XO1.CanvasOperations.PreparedCanvasAll(C);
                    double Scale = 3;
                    BackSizeParts2.Children.Add(C);
                    Canvas.SetTop(C, 900);
                    if (C.Height * Scale > 250)
                    {
                        Scale = Scale * (250 / (C.Height * Scale));
                    }
                    Scale = Math.Min(760 / C.Width, Scale);
                    ScaleTransform ST = new ScaleTransform(Scale / 2, Scale / 2);
                    RotateTransform RT = new RotateTransform();
                    Vector V = new Vector(1, 0);
                    Point P1 = new Point(PW.Part.PartType.FrontLeftPoints[0].X, PW.Part.PartType.FrontLeftPoints[0].Y);
                    Point P2 = new Point(PW.Part.PartType.FrontLeftPoints[1].X, PW.Part.PartType.FrontLeftPoints[1].Y);
                    double Angle = Vector.AngleBetween(new Vector(P1.X - P2.X, P1.Y - P2.Y), V);
                    if (Angle > 90)
                        Angle = 180 - Angle;
                    RT.Angle = -30;
                    TransformGroup TG = new TransformGroup();
                    TG.Children.Add(RT);
                    TG.Children.Add(ST);
                    C.RenderTransform = TG;
                    Canvas.SetLeft(C, 26);

                    FlowDocumentScrollViewer FDSV = new FlowDocumentScrollViewer();
                    FDSV.IsToolBarVisible = false;
                    FDSV.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                    FDSV.PreviewMouseWheel += new MouseWheelEventHandler(BackSizeGrid2MouseWheel);
                    FDSV.Zoom = 1;
                    FDSV.ZoomIncrement = 0.0000001;
                    Table T = this.Project.ApaturaTable;
                    FlowDocument FD = new FlowDocument();
                    FD.Blocks.Add(T);
                    FDSV.Document = FD;
                    FDSV.RenderTransform = new ScaleTransform(1.2, 1);
                    Canvas.SetTop(FDSV, 800);
                    Canvas.SetLeft(FDSV, 300);
                    BackSizeParts2.Children.Add(FDSV);
                }
            }
        }
        private void FillBackSize2Template()
        {
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("DetailViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p =>
            {
                if (p.Name == "DetailName")
                {
                    p.Text = "Gömlek Arka Beden";
                }
                else if (p.Name == "Customer_RepresentativeDetail")
                {
                    p.Text = Customer_Representative.Text;
                }
                else if (p.Name == "PlanDetail")
                {
                    p.Text = Plan.Text;
                }
                else if (p.Name == "Technical_DrawingDetail")
                {
                    p.Text = Technical_Drawing.Text;
                }
            });
            BackSizeTemplateCanvas2.Children.Clear();
            BackSizeTemplateCanvas2.Children.Add(Template);

        }
        private void FillBackSize2()
        {
            FillBackSize2PartCanvas();
            FillBackSize2Template();
        }
        private void BackSizeGrid2MouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(BackSizeShapes2, BackSizeStackPanel2, BackSizeGrid2);
        }
        private void BackSizeGrid2MouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(BackSizeStackPanel2, e.Delta > 0, e.GetPosition(BackSizeStackPanel2));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void BackSizeGrid2MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(BackSizeShapes2, BackSizeStackPanel2);
        }
        private void BackSizeGrid2MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(BackSizeShapes2);
        }   
        #endregion BackSize2

        #region Iron
        private void FillIronPartCanvas()
        { 
           /* List<pvdcd.Part> PL=new List<pvdcd.Part>();
            foreach (pvdcd.PartWithProperty PW in this.Project.PartList)
            {
                PL.Add(PW.Part);
            }
            Canvas Front = JoinPartsFront(PL);
            Canvas Back = JoinPartsBack(PL);
            Front = XamlOperations.CanvasOperations.PreparedCanvasAll(Front);
            Back = XamlOperations.CanvasOperations.PreparedCanvasAll(Back);*/
        }
        private void FillIronTemplate()
        {
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("IronViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p =>
            {
                if (p.Name == "Customer_RepresentativeDetailU")
                {
                    p.Text = Customer_Representative.Text;
                }
                else if (p.Name == "PlanDetailU")
                {
                    p.Text = Plan.Text;
                }
                else if (p.Name == "Technical_DrawingDetailU")
                {
                    p.Text = Technical_Drawing.Text;
                }
            });
            IronTemplateCanvas.Children.Clear();
            IronTemplateCanvas.Children.Add(Template);
            

        }
        private void FillIron()
        {
            FillIronPartCanvas();
            FillIronTemplate();
        }
        private void IronFirstOpen()
        {
            int dept = 575;
            for (int i = 0; i < 12; ++i)
            {
                if (i < 5)
                    dept = dept + 25;
                else if (i == 5)
                    dept = 800;
                else if (i < 7)
                    dept = dept + 25;
                else if (i == 7)
                    dept = 875;
                else if (i < 10)
                    dept = dept + 25;
                else if (i == 10)
                    dept = 975;
                else if (i == 11)
                    dept = 1000;
                FillIronTemplate();
                RichTextBox T = new RichTextBox();
                T.AppendText("......");
                T.FontSize = 15;
                Canvas.SetTop(T, dept+5);
                Canvas.SetLeft(T, 350);
                T.Width = 400;
                ((Canvas)(IronShapes.Children[0])).Children.Add(T);
                T.BorderBrush = Brushes.White;
            }
        }
        private void FillEveryThing()
        {
            FillGeneralView();
            FillCollar();
            FillFrontSize();
            FillBackSize1();
            FillBackSize2();
            FillIron();
        }
        private void IronGridMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(IronShapes, IronStackPanel, IronGrid);
        }
        private void IronGridMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(IronStackPanel, e.Delta > 0, e.GetPosition(IronStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void IronGridMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(IronShapes, IronStackPanel);
        }
        private void IronGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(IronShapes);
        }
        #endregion Iron

        //Events//
        private void RemoveSeamClick(object sender, RoutedEventArgs e)
        {
            MenuItem MI = sender as MenuItem;
            Button B = (Button)(((ContextMenu)(MI.Parent)).PlacementTarget);
            ((pvdcd.PartWithProperty)(B.Tag)).Seam = false;
        }
        private void BringSeamClick(object sender, RoutedEventArgs e)
        {
            MenuItem MI = sender as MenuItem;
            Button B = (Button)(((ContextMenu)(MI.Parent)).PlacementTarget);
            ((pvdcd.PartWithProperty)(B.Tag)).Seam = true;
        }
        private void AddItem(object sender, RoutedEventArgs e)
        {
            Button B=sender as Button;
            pvdcd.PartWithProperty PWP = (pvdcd.PartWithProperty)(B.Tag);
            foreach (pvdcd.PartWithProperty P in Project.PartList)
            {
                if (P.Part.PartType.PartType == PWP.Part.PartType.PartType)
                {
                    Project.PartList.Remove(P);
                    break;
                }
            }
            Project.PartList.Add(PWP);
            FillEveryThing();
            return;
        }
        private void RemoveItem(object sender, RoutedEventArgs e)
        {
            Button B = sender as Button;
            Template.Type T = (Template.Type)(B.Tag);
            foreach (pvdcd.PartWithProperty P in Project.PartList)
            {
                if (P.Part.PartType.PartType == T.PartType)
                {
                    Project.PartList.Remove(P);
                    break;
                }
            }
            FillEveryThing();
            return;
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            foreach (Window a in Application.Current.Windows)
            {
                try
                {
                    a.Visibility = Visibility.Visible;
                }
                catch { }
            }
        }
        private void TemplateComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FillPartTabs();
        }
        private void OpenProject(object sender, RoutedEventArgs e)
        {
            if (!System.IO.Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\Projects\\"))
                System.IO.Directory.CreateDirectory(System.IO.Directory.GetCurrentDirectory() + "\\Projects\\");
            System.IO.Stream MyStream = null;
            System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
            ofld.Filter = "prj files (*.prj)|*.prj*";
            ofld.FilterIndex = 2;
            ofld.RestoreDirectory = false;
            ofld.InitialDirectory = System.IO.Directory.GetCurrentDirectory() + "\\Projects\\";
            if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if ((MyStream = ofld.OpenFile()) != null)
                {
                    MyStream.Close();
                    String FileName = ofld.FileName;
                    if (System.IO.Path.GetExtension(FileName) == ".prj")
                    {
                        this.Project = Project.Load(FileName);
                         //Activate this Field if changed on Parts is applied to project
                         /*foreach (pvdcd.PartWithProperty PWP in PartWithPropertyList)
                         {
                            foreach (pvdcd.PartWithProperty PWP1 in this.Project.PartList)
                               if (PWP.Part.TemplateName == PWP1.Part.TemplateName && PWP.Part.PartType.PartType == PWP1.Part.PartType.PartType && PWP.Part.PartName == PWP1.Part.PartName)
                                   PWP1.Part = PWP.Part;
                         }*/
                         //Activate this Field if changed on Parts is applied to project
                    }
                    FillEveryThing();
                    PreparedBy.Text = this.Project.PreparedBy;
                    ProjectName.Text = this.Project.ProjectName;
                    Customer_Representative.Text = this.Project.Customer_Representative;
                    Plan.Text = this.Project.Plan;
                    Technical_Drawing.Text = this.Project.Technical_Drawing;
                    GeneralViewShapes.Children.Clear();
                    GeneralViewShapes.Children.Add(this.Project.ShapeGeneralView);
                    CollarShapes.Children.Clear();
                    CollarShapes.Children.Add(this.Project.ShapeCollar);
                    FrontSizeShapes.Children.Clear();
                    FrontSizeShapes.Children.Add(this.Project.ShapeFrontSize);
                    BackSizeShapes1.Children.Clear();
                    BackSizeShapes1.Children.Add(this.Project.ShapeBackSize1);
                    BackSizeShapes2.Children.Clear();
                    BackSizeShapes2.Children.Add(this.Project.ShapeBackSize2);
                    IronShapes.Children.Clear();
                    IronShapes.Children.Add(this.Project.ShapeIron);
                    AddTextBoxEvents((Canvas)IronShapes.Children[0]);
                    AddTextBoxEvents((Canvas)GeneralViewShapes.Children[0]);
                    AddTextBoxEvents((Canvas)CollarShapes.Children[0]);
                    AddTextBoxEvents((Canvas)FrontSizeShapes.Children[0]);
                    AddTextBoxEvents((Canvas)BackSizeShapes1.Children[0]);
                    AddTextBoxEvents((Canvas)BackSizeShapes2.Children[0]);
                    
                }
            }
        }
        private void SaveProject(object sender, RoutedEventArgs e)
        {
            //Project Save Checks
            if (this.PreparedBy.Text == "")
            {
                MessageBox.Show("Hazırlayan Kısmına İsminize Giriniz!");
                return;
            }
            if (this.ProjectName.Text == "")
            {
                MessageBox.Show("Proje İsmini Giriniz!");
                return;
            }
            this.Project.Customer_Representative = Customer_Representative.Text;
            this.Project.Plan = Plan.Text;
            this.Project.Technical_Drawing = Technical_Drawing.Text;
            this.Project.PreparedBy = PreparedBy.Text;
            this.Project.ProjectName = ProjectName.Text;
            this.Project.ShapeGeneralView = (Canvas)GeneralViewShapes.Children[0];
            this.Project.ShapeCollar = (Canvas)CollarShapes.Children[0];
            this.Project.ShapeFrontSize = (Canvas)FrontSizeShapes.Children[0];
            this.Project.ShapeBackSize1 = (Canvas)BackSizeShapes1.Children[0];
            this.Project.ShapeBackSize2 = (Canvas)BackSizeShapes2.Children[0];
            this.Project.ShapeIron = (Canvas)IronShapes.Children[0];

            this.Project.Save();
            MessageBox.Show("Proje Kaydedildi");
        }

        #region Print
        private void PrintClick(object sender, RoutedEventArgs e)
        {
            PrintDialog PD = new PrintDialog();
            PD.UserPageRangeEnabled = true;
            if (PD.ShowDialog() == true)
            {
                FixedDocument FD = new FixedDocument();
                //GeneralView
                FixedPage FPGeneralView = new FixedPage();
                PageContent PCGeneralView = new PageContent();
                FPGeneralView.Width = 793.2774;
                FPGeneralView.Height = 1122.72;
                UIElement UI = XamlEditor.CloneUIElement.cloneElement(GeneralViewStackPanel);
                UI.RenderTransform = null;
                FPGeneralView.Children.Add(UI);
                PCGeneralView.Child = FPGeneralView;
                FD.Pages.Add(PCGeneralView);
                //

                //CollarPage
                FixedPage FPCollarView = new FixedPage();
                PageContent PCCollarView = new PageContent();
                FPCollarView.Width = 793.2774;
                FPCollarView.Height = 1122.72;
                UIElement UIC = XamlEditor.CloneUIElement.cloneElement(CollarStackPanel);
                UIC.RenderTransform = null;
                FPCollarView.Children.Add(UIC);
                PCCollarView.Child = FPCollarView;
                FD.Pages.Add(PCCollarView);
                //

                //FrontSize
                FixedPage FPFrontSizeView = new FixedPage();
                PageContent PCFrontSizeView = new PageContent();
                FPFrontSizeView.Width = 793.2774;
                FPFrontSizeView.Height = 1122.72;
                UIElement UIF = XamlEditor.CloneUIElement.cloneElement(FrontSizeStackPanel);
                UIF.RenderTransform = null;
                FPFrontSizeView.Children.Add(UIF);
                PCFrontSizeView.Child = FPFrontSizeView;
                FD.Pages.Add(PCFrontSizeView);
                //

                //BackSize1
                FixedPage FPBackSizeView = new FixedPage();
                PageContent PCBackSizeView = new PageContent();
                FPBackSizeView.Width = 793.2774;
                FPBackSizeView.Height = 1122.72;
                UIElement UIB = XamlEditor.CloneUIElement.cloneElement(BackSizeStackPanel1);
                UIB.RenderTransform = null;
                FPBackSizeView.Children.Add(UIB);
                PCBackSizeView.Child = FPBackSizeView;
                FD.Pages.Add(PCBackSizeView);
                //

                //BackSize2
                FixedPage FPBackSizeView1 = new FixedPage();
                PageContent PCBackSizeView1 = new PageContent();
                FPBackSizeView1.Width = 793.2774;
                FPBackSizeView1.Height = 1122.72;
                UIElement UIB1 = XamlEditor.CloneUIElement.cloneElement(BackSizeStackPanel2);
                UIB1.RenderTransform = null;
                FPBackSizeView1.Children.Add(UIB1);
                PCBackSizeView1.Child = FPBackSizeView1;
                FD.Pages.Add(PCBackSizeView1);
                //

                //BackSize1
                FixedPage FPIronView = new FixedPage();
                PageContent PCIronView = new PageContent();
                FPIronView.Width = 793.2774;
                FPIronView.Height = 1122.72;
                UIElement UII = XamlEditor.CloneUIElement.cloneElement(IronStackPanel);
                UII.RenderTransform = null;
                FPIronView.Children.Add(UII);
                PCIronView.Child = FPIronView;
                FD.Pages.Add(PCIronView);
                //

                DocumentReference DR = new DocumentReference();
                DR.SetDocument(FD);
                FixedDocumentSequence FDS = new FixedDocumentSequence();
                FDS.References.Add(DR);
                PD.PrintDocument(FDS.DocumentPaginator, "Proje");
            }
        }
        private Canvas GetFrontCanvas(double Width, double Height)
        {
            Canvas FrontView = new Canvas();
            FrontView.Width = Width;
            FrontView.Height = Height;
            double WidthC=909;
            double HeightC=1350;
            if (Templates.Count > 0)
            {
                //GEt Canvas With Height//
                WidthC = Templates[0].ModelArka.Width;
                HeightC = Templates[0].ModelOn.Height;
            }
            Canvas CF = new Canvas();
            CF.Width = WidthC;
            CF.Height = HeightC;
            CF.RenderTransform = new ScaleTransform(Math.Min(Width / WidthC, Height / HeightC), Math.Min(Width / WidthC, Height / HeightC));
            if (Width / WidthC > Height / HeightC)
            {
                FrontView.Children.Add(CF);
                Canvas.SetLeft(CF, (Width - (WidthC * Height / HeightC)) / 2);
            }
            else
            {
                FrontView.Children.Add(CF);
                Canvas.SetTop(CF, (Height - (HeightC * Width / WidthC)) / 2);
            }
            return FrontView;
        }
        private Canvas GetBackCanvas(double Width, double Height)
        {
            Canvas BackView = new Canvas();
            BackView.Width = Width;
            BackView.Height = Height;
            double WidthC = 909;
            double HeightC = 1350;
            if (Templates.Count > 0)
            {
                //GEt Canvas With Height//
                WidthC = Templates[0].ModelArka.Width;
                HeightC = Templates[0].ModelOn.Height;
            }
            Canvas CB = new Canvas();
            CB.Width = WidthC;
            CB.Height = HeightC;
            CB.RenderTransform = new ScaleTransform(Math.Min(Width / WidthC, Height / HeightC), Math.Min(Width / WidthC, Height / HeightC));
            if (Width / WidthC > Height / HeightC)
            {
                BackView.Children.Add(CB);
                Canvas.SetLeft(CB, (Width - (WidthC * Height / HeightC)) / 2);
            }
            else
            {
                BackView.Children.Add(CB);
                Canvas.SetTop(CB, (Height - (HeightC * Width / WidthC)) / 2);
            }
            return BackView;
        }
        private Canvas ModelGeneralView(double Width, double Height)
        {
            Canvas GeneralView = new Canvas();
            Canvas FrontView = GetFrontCanvas(Width, Height);
            Canvas BackView = GetBackCanvas(Width, Height);
            Canvas Template = (Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GeneralViewTemplate"));
            Template.Children.OfType<TextBlock>().ToList<TextBlock>().ForEach(p => 
            {
                if (p.Name == "Date")
                { 
                    DateTime T=DateTime.Now;
                    p.Text = "Tarih: " + String.Format("{0:dd/MM/yyyy}", T); 
                }
            });
            GeneralView.Width = Width;
            GeneralView.Height = Height;
            GeneralView.Children.Add(FrontView);
            GeneralView.Children.Add(BackView);
            GeneralView.Children.Add(Template);
            Template.RenderTransform = new ScaleTransform(Width / Template.Width, Height/Template.Height);
            FrontView.RenderTransform = new ScaleTransform(0.5, 0.5);
            BackView.RenderTransform = new ScaleTransform(0.5, 0.5);
            Canvas.SetLeft(BackView, Width / 20*9);
            Canvas.SetLeft(FrontView, Width / 20);
            Canvas.SetTop(FrontView, Height / 6);
            Canvas.SetTop(BackView, Height / 3);

            return GeneralView;
        }
        #endregion Print

        private void NakısEkleClick(object sender, RoutedEventArgs e)
        {
            EscapeKey();
            System.IO.Stream myStream = null;
            System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
            ofld.Filter = "jpeg files (*.jpeg)|*.jpeg|jpg files (*.jpg)|*.jpg|png files (*.png)|*.png";
            ofld.FilterIndex = 2;
            ofld.RestoreDirectory = true;
            if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Image I = new Image();
                if ((myStream = ofld.OpenFile()) != null)
                {
                    myStream.Close();
                    Image AddImage = new Image();
                    AddImage.Source = BitmapFrame.Create(new Uri(ofld.FileName));
                    AddImage.RenderTransform = new ScaleTransform(1, 1);
                    Canvas.SetLeft(AddImage, 0);
                    Canvas.SetTop(AddImage, 0);
                    if(ProjectTABS.SelectedItem==GeneralViewTabItem)
                        ((Canvas)(GeneralViewShapes.Children[0])).Children.Insert(0, AddImage);
                    else if(ProjectTABS.SelectedItem==CollarTabItem)
                        ((Canvas)(CollarShapes.Children[0])).Children.Insert(0, AddImage);
                    else if (ProjectTABS.SelectedItem == FrontSize)
                        ((Canvas)(FrontSizeShapes.Children[0])).Children.Insert(0, AddImage);
                    else if (ProjectTABS.SelectedItem == BackSize1)
                        ((Canvas)(BackSizeShapes1.Children[0])).Children.Insert(0, AddImage);
                    else if (ProjectTABS.SelectedItem == BackSize2)
                        ((Canvas)(BackSizeShapes2.Children[0])).Children.Insert(0, AddImage);
                    else if (ProjectTABS.SelectedItem == Iron)
                        ((Canvas)(IronShapes.Children[0])).Children.Insert(0, AddImage);
                }
            }
        }
        private void NoteEkleClick(object sender, RoutedEventArgs e)
        {
            Aim = "Text";
        }
        private void NoteRemove(object sender, RoutedEventArgs e)
        {
            MenuItem MI=sender as MenuItem;
            UIElement UI=(UIElement)(((ContextMenu)(MI.Parent)).PlacementTarget);
            ((Canvas)(((TextBox)UI).Parent)).Children.Remove(UI);
            EscapeKey();
        }
        private void AddPatClick(object sender, RoutedEventArgs e)
        {
            Aim = "Pat";
        }
        private void AddPathClick(object sender, RoutedEventArgs e)
        {
            Aim = "Path";
        }
        private void ArrowAddClick(object sender, RoutedEventArgs e)
        {
            Aim = "Arrow";
        }
        private void DoubleArrowAddClick(object sender, RoutedEventArgs e)
        {
            Aim = "DoubleArrow";
        }
        private void AddSeamClick(object sender, RoutedEventArgs e)
        {
            Aim = "Seam";
        }
        private void TextBoxMouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            TextBox T=sender as TextBox;
            EscapeKey();
            SelectedUIElement = T;
            pd1.SelectedObject = T;
            AddAdorner();
        }
        private void TabControlSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EscapeKey();
        }
        //MouseEvents

        private void GridMouseMove(Canvas C, StackPanel SP, Grid G)
        {
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
            if (Aim == "Text")
            {
                this.Cursor = Cursors.Pen;
            }
            else
            { this.Cursor = Cursors.Arrow; }
            if ((Aim == "Seam"||Aim=="Path") && SeamNodes.Count > 0)
            {
                if (Mouse.LeftButton == MouseButtonState.Pressed)
                {
                    PathMaker.Node A = SeamNodes[SeamNodes.Count - 1];
                    A.Nodetype = PathMaker.Node.NodeType.Symetrical;
                    A.ControlPoint2 = Mouse.GetPosition(C);

                    List<PathMaker.Node> NL = new List<PathMaker.Node>();
                    foreach (PathMaker.Node N in SeamNodes)
                    {
                        NL.Add(N);
                    }
                    NL.Add(new PathMaker.Node(NP: Mouse.GetPosition(C)));
                    RemoveSeam.Data = PathMaker.Node.GetPath(NL);
                    if (Aim == "Seam")
                    {
                        RemoveSeam.Stroke = Brushes.Red;
                        RemoveSeam.StrokeDashArray = new DoubleCollection() { 4, 2 };
                        RemoveSeam.StrokeThickness = 1;
                    }
                    else if (Aim == "Path")
                    {
                        RemoveSeam.Stroke = Brushes.Black;
                        RemoveSeam.StrokeThickness = 2;
                        RemoveSeam.ClearValue(Path.StrokeDashArrayProperty);
                    }
                    try { ((Canvas)(C.Children[0])).Children.Add(RemoveSeam); }
                    catch { }
                }
                else
                {
                    List<PathMaker.Node> NL = new List<PathMaker.Node>();
                    foreach (PathMaker.Node N in SeamNodes)
                    {
                        NL.Add(N);
                    }
                    NL.Add(new PathMaker.Node(NP: Mouse.GetPosition(C)));
                    RemoveSeam.Data = PathMaker.Node.GetPath(NL);
                    if (Aim == "Seam")
                    {
                        RemoveSeam.Stroke = Brushes.Red;
                        RemoveSeam.StrokeDashArray = new DoubleCollection() { 4, 2 };
                        RemoveSeam.StrokeThickness = 1;
                    }
                    else if (Aim == "Path")
                    {
                        RemoveSeam.Stroke = Brushes.Black;
                        RemoveSeam.StrokeThickness = 2;
                        RemoveSeam.ClearValue(Path.StrokeDashArrayProperty);
                    }
                    try { ((Canvas)(C.Children[0])).Children.Add(RemoveSeam); }
                    catch { }
                }
            }
            if (Mouse.MiddleButton == MouseButtonState.Pressed)
            {
                if (MidPointPosition != null)
                {
                    Point p = Mouse.GetPosition(G);
                    XO1.TransformationOperations.MoveUIElement(SP, p.X - MidPointPosition.Value.X, p.Y - MidPointPosition.Value.Y);
                    MidPointPosition = p;
                }
                else
                {
                    MidPointPosition = Mouse.GetPosition(G);
                }

            }
            else
            {
                MidPointPosition = Mouse.GetPosition(G);
                if (Aim == "Arrow" && FirstPoint != null)
                {
                    RemoveLineArrow.StartPoint = (Point)FirstPoint;
                    RemoveLineArrow.EndPoint = Mouse.GetPosition(C);
                    RemoveLineArrow.Caps = LineWithArrows.LineArrow.ArrowEnds.End;
                    RemoveLineArrow.Stroke = Brushes.Black;
                    try
                    {
                        ((Canvas)(C.Children[0])).Children.Add(RemoveLineArrow);
                    }
                    catch { }

                }
                else if (Aim == "DoubleArrow" && FirstPoint != null)
                {
                    RemoveLineArrow.StartPoint = (Point)FirstPoint;
                    RemoveLineArrow.EndPoint = Mouse.GetPosition(C);
                    RemoveLineArrow.Caps = LineWithArrows.LineArrow.ArrowEnds.Both;
                    RemoveLineArrow.Stroke = Brushes.Black;
                    try
                    {
                        ((Canvas)(C.Children[0])).Children.Add(RemoveLineArrow);
                    }
                    catch { }
                }
                else if (Aim == "Pat" && FirstPoint != null)
                {
                    RemovePat.StartPoint = (Point)FirstPoint;
                    RemovePat.EndPoint = Mouse.GetPosition(C);
                    RemovePat.Stroke = Brushes.Black;
                    try
                    {
                        ((Canvas)(C.Children[0])).Children.Add(RemovePat);
                    }
                    catch { }
                }
            }
        }
        private void GridMouseLeftButtonDown(Canvas C,StackPanel SP)
        {
            if (Aim == "Text")
            {
                TextBox T = new TextBox();
                T.Background = Brushes.Transparent;
                T.Height = 20;
                T.Width = 80;
                Canvas.SetLeft(T, Mouse.GetPosition(SP).X);
                Canvas.SetTop(T, Mouse.GetPosition(SP).Y);
                T.Text = "Note";
                T.BorderBrush = Brushes.White;

                //
                MenuItem MenuItem1 = new MenuItem();
                MenuItem1.Header = "Notu Sil";
                MenuItem1.Click += new RoutedEventHandler(NoteRemove);
                MenuItem1.Icon = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("Images/Delete.png", UriKind.Relative))
                };
                MenuItem1.Height = 20;
                T.ContextMenu = new ContextMenu();
                T.ContextMenu.Items.Add(MenuItem1);
                //
                T.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(TextBoxMouseLeftButtonDown);
                ((Canvas)(C.Children[0])).Children.Add(T);
                RemoveAdorner();
                SelectedUIElement = T;
                pd1.SelectedObject = T;
                AddAdorner();
                this.Cursor = Cursors.Arrow;
                Aim = "";
            }
            else if ((Aim == "Arrow" || Aim == "DoubleArrow" || Aim == "Pat") && FirstPoint == null)
            {

                FirstPoint = Mouse.GetPosition(C);
                return;

            }
            else if ((Aim == "Arrow" || Aim == "DoubleArrow" || Aim == "Pat") && FirstPoint != null)
            {
                if (Aim == "Arrow")
                {
                    LineWithArrows.LineArrow A = new LineWithArrows.LineArrow();
                    A.StartPoint = (Point)FirstPoint;
                    A.EndPoint = Mouse.GetPosition(C);
                    A.Caps = LineWithArrows.LineArrow.ArrowEnds.End;
                    A.Stroke = Brushes.Black;
                    ((Canvas)(C.Children[0])).Children.Add(A);
                    RemoveAdorner();
                    SelectedUIElement = A;
                    pd1.SelectedObject = A;
                    AddAdorner();
                    try { ((Canvas)(RemoveLineArrow.Parent)).Children.Remove(RemoveLineArrow); }
                    catch { }
                }
                else if (Aim == "DoubleArrow")
                {
                    LineWithArrows.LineArrow A = new LineWithArrows.LineArrow();
                    A.StartPoint = (Point)FirstPoint;
                    A.EndPoint = Mouse.GetPosition(C);
                    A.Caps = LineWithArrows.LineArrow.ArrowEnds.Both;
                    A.Stroke = Brushes.Black;
                    ((Canvas)(C.Children[0])).Children.Add(A);
                    RemoveAdorner();
                    SelectedUIElement = A;
                    pd1.SelectedObject = A;
                    AddAdorner();
                    try { ((Canvas)(RemoveLineArrow.Parent)).Children.Remove(RemoveLineArrow); }
                    catch { }
                }
                else if (Aim == "Pat")
                {
                    Pats.Pat A = new Pats.Pat();
                    A.StartPoint = (Point)FirstPoint;
                    A.EndPoint = Mouse.GetPosition(C);
                    A.Stroke = Brushes.Black;
                    ((Canvas)(C.Children[0])).Children.Add(A);
                    RemoveAdorner();
                    SelectedUIElement = A;
                    pd1.SelectedObject = A;
                    AddAdorner();
                    try { ((Canvas)(RemovePat.Parent)).Children.Remove(RemovePat); }
                    catch { }
                }

                Aim = "";
                FirstPoint = null;
            }
            else if (Aim == "Seam" || Aim == "Path")
            {
                SeamNodes.Add(new PathMaker.Node(NP: Mouse.GetPosition(C)));
            }
            else
            {
                Point ClickPoint = Mouse.GetPosition(C);
                Rect Rec = new Rect(new Point(ClickPoint.X - 5, ClickPoint.Y - 5), new Point(ClickPoint.X + 5, ClickPoint.Y + 5));
                ((Canvas)(C.Children[0])).Children.OfType<UIElement>().ToList<UIElement>().ForEach
                (p =>
                {
                    if (p is LineWithArrows.LineArrow)
                    {
                        Rect RecP = new Rect(((LineWithArrows.LineArrow)p).StartPoint, ((LineWithArrows.LineArrow)p).EndPoint);
                        if (RecP.IntersectsWith(Rec))
                        {
                            RemoveAdorner();
                            SelectedUIElement = p;
                            pd1.SelectedObject = p;
                            AddAdorner();
                            return;
                        }
                    }
                    else if (p is Image)
                    {
                        double SX = 1;
                        double SY = 1;
                        if (p.RenderTransform is ScaleTransform)
                        {
                            SX = ((ScaleTransform)(p.RenderTransform)).ScaleX;
                            SY = ((ScaleTransform)(p.RenderTransform)).ScaleY;
                        }
                        if (((Image)p).Source != null)
                        {
                            Rect RecP = new Rect(new Point(Canvas.GetLeft(p), Canvas.GetTop(p)), new Point(((Image)p).Source.Width * SX + Canvas.GetLeft(p), ((Image)p).Source.Height * SY + Canvas.GetTop(p)));
                            if (RecP.IntersectsWith(Rec))
                            {
                                RemoveAdorner();
                                SelectedUIElement = p;
                                pd1.SelectedObject = p;
                                AddAdorner();
                                return;
                            }
                        }
                    }
                    else if (p is Pats.Pat)
                    {
                        Rect RecP = new Rect(((Pats.Pat)p).StartPoint, ((Pats.Pat)p).EndPoint);
                        if (RecP.IntersectsWith(Rec))
                        {
                            RemoveAdorner();
                            SelectedUIElement = p;
                            pd1.SelectedObject = p;
                            AddAdorner();
                            return;
                        }
                    }
                    else if (p is Path)
                    {
                        if (Rec.IntersectsWith(((Path)p).TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds)))
                        {
                            RemoveAdorner();
                            SelectedUIElement = p;
                            pd1.SelectedObject = p;
                            AddAdorner();
                            return;
                        }
                    }
                });
            }
        }
        private void GridMouseLeftButtonUp(Canvas C)
        {
            if ((Aim == "Seam"||Aim=="Path") && SeamNodes.Count > 2)
            {
                Path S = new Path();
                S.Data = PathMaker.Node.GetPath(SeamNodes);
                if (Aim == "Seam")
                {
                    S.Stroke = Brushes.Red;
                    S.StrokeDashArray = new DoubleCollection() { 4, 2 };
                    S.StrokeThickness = 1;
                }
                else if (Aim == "Path")
                {
                    S.Stroke = Brushes.Black;
                    S.StrokeThickness = 2;
                    S.ClearValue(Path.StrokeDashArrayProperty);
                }
                try { ((Canvas)(C.Children[0])).Children.Remove(RemoveSeam); }
                catch { }
                try
                {
                    ((Canvas)(C.Children[0])).Children.Remove(
                        ((Canvas)(C.Children[0])).Children.OfType<Path>().ToList<Path>().Last<Path>()
                                                                            );
                }
                catch { }
                ((Canvas)(C.Children[0])).Children.Add(S);
            }
            else if ((Aim == "Seam"||Aim=="Path") && SeamNodes.Count > 1)
            {
                Path S = new Path();
                S.Data = PathMaker.Node.GetPath(SeamNodes);
                if (Aim == "Seam")
                {
                    S.Stroke = Brushes.Red;
                    S.StrokeDashArray = new DoubleCollection() { 4, 2 };
                    S.StrokeThickness = 1;
                }
                else if (Aim == "Path")
                {
                    S.Stroke = Brushes.Black;
                    S.StrokeThickness = 2;
                    S.ClearValue(Path.StrokeDashArrayProperty);
                }
                try { ((Canvas)(C.Children[0])).Children.Remove(RemoveSeam); }
                catch { }
                ((Canvas)(C.Children[0])).Children.Add(S);
            }
        }

        //Key Events
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                EscapeKey();
            else if (e.Key == Key.Delete)
                DeleteKey();
            else if (e.Key == Key.F1)
            {
                pvdcd.Project.OpenHelp();
            }
        }
        private void EscapeKey()
        {
            RemoveAdorner();
            Aim = "";
            SelectedUIElement = null;
            FirstPoint = null;
            SeamNodes.Clear();
            try { ((Canvas)(RemoveLineArrow.Parent)).Children.Remove(RemoveLineArrow); }
            catch { }
            try { ((Canvas)(RemovePat.Parent)).Children.Remove(RemovePat); }
            catch { }
            try { ((Canvas)(RemoveSeam.Parent)).Children.Remove(RemoveSeam); }
            catch { }
            this.Cursor = Cursors.Arrow;
            pd1.SelectedObject = null;
            
        }
        private void DeleteKey()
        {
            if (SelectedUIElement != null)
            {
                ((Canvas)(GeneralViewShapes.Children[0])).Children.Remove(SelectedUIElement);
                ((Canvas)(CollarShapes.Children[0])).Children.Remove(SelectedUIElement);
                ((Canvas)(FrontSizeShapes.Children[0])).Children.Remove(SelectedUIElement);
                ((Canvas)(BackSizeShapes1.Children[0])).Children.Remove(SelectedUIElement);
                ((Canvas)(BackSizeShapes2.Children[0])).Children.Remove(SelectedUIElement);
                ((Canvas)(IronShapes.Children[0])).Children.Remove(SelectedUIElement);
            }
            EscapeKey();
        }

        #region TextChange

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            FillGeneralViewTemplate();
            FillFrontSizeTemplate();
            FillCollarTemplate();
            FillFrontSizeTemplate();
            FillBackSize1Template();
            FillBackSize2Template();
            FillIronTemplate();
        }

        #endregion //TextChange
    }
}
